<?php


namespace api\modules\v1\controllers\buss;

use common\models\buss\AccountsData;
use services\buss\AccountsService;
use services\buss\DoctorsService;
use services\buss\PlatformService;
use services\member\MemberService;
use Yii;
use api\controllers\OnAuthController;
use common\helpers\CommonHelper;
use services\buss\AccountsDataService;

class AccountsDataController extends OnAuthController
{
    public $modelClass = '';

    /**
     * @api {get} /v1/buss/accounts-data/index 业务数据列表
     * @apiGroup AccountsData
     * @apiName index
     * @apiUse CommonParam
     *
     * @apiParam {String} date_time                  日期
     * @apiParam {Number} platform_id                平台ID
     * @apiParam {Number} doctor_id                  医生ID
     * @apiParam {int} page
     * @apiParam {int} pageSize
     *
     * @apiSuccess {Object}  data
     * @apiSuccess {Array}   data.list                              列表数据
     * @apiSuccess {String}  data.list.date_stime                   开始日期
     * @apiSuccess {String}  data.list.date_etime                   结束日期
     * @apiSuccess {Number}  data.list.platform_id
     * @apiSuccess {String}  data.list.platform_name                平台
     * @apiSuccess {Number}  data.list.account_id
     * @apiSuccess {String}  data.list.account_name                 账号
     * @apiSuccess {String}  data.list.doctor_name                  医生
     * @apiSuccess {Number}  data.list.private_sms                  私信
     * @apiSuccess {Number}  data.list.clue                         线索
     * @apiSuccess {Number}  data.list.effective_clue               有效线索
     * @apiSuccess {Number}  data.list.arrive                       到诊
     * @apiSuccess {String}  data.list.use_mname                    运营人
     * @apiSuccess {String}  data.list.created_at
     * @apiSuccess {String}  data.list.updated_at
     * @apiSuccess {String}  data.condition
     * @apiSuccess {String}  data.condition.platform_arr                    平台筛选
     * @apiSuccess {Number}  data.condition.platform_arr.value
     * @apiSuccess {String}  data.condition.platform_arr.label
     * @apiSuccess {String}  data.condition.doctors_arr                     认证医生筛选
     * @apiSuccess {Number}  data.condition.doctors_arr.value
     * @apiSuccess {String}  data.condition.doctors_arr.label
     * @apiSuccess {String}  data.condition.department_arr                  部门列表
     *
     */
    public function actionIndex(): array
    {
        $params = Yii::$app->request->get();

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;
        $accountService = new AccountsDataService();
        $list = $accountService->getList($params, $page, $pageSize);

        // 筛选项
        $list['condition'] = [
            'platform_arr' => PlatformService::getListCondition(),
            'doctors_arr' => DoctorsService::getListCondition(),
            'department_arr' => MemberService::getMemberDepartmentCondition()['list'],
        ];
        return $list;
    }

    /**
     * @api {get} /v1/buss/accounts-data/edit-condition 业务数据下拉筛选
     * @apiGroup AccountsData
     * @apiName edit-condition
     * @apiUse CommonParam
     *
     * @apiParam {Number} id                    业务数据ID
     * @apiParam {Number} platform_id           平台ID【传时返回账号，不传返回平台】
     *
     * @apiSuccess {String}  data.condition
     * @apiSuccess {String}  data.condition.platform_arr                    平台筛选
     * @apiSuccess {String}  data.condition.accounts_arr                    平台下账号筛选
     *
     */
    public function actionEditCondition(): array
    {
        $params = Yii::$app->request->get();
        $params['id'] = $params['id'] ?? 0;
        $accountsData = AccountsData::findOne($params['id']);
        $params['platform_id'] = $params['platform_id'] ?? 0;
        // 返回平台筛选
        $list['condition'] = [
            'platform_arr' => PlatformService::getListCondition(),
            'accounts_arr' => $params['platform_id'] ? AccountsService::getEditCondition((int)$params['platform_id'], $accountsData->account_id ?? null) : [],
        ];
        return $list;
    }

    /**
     * @api {post} /v1/buss/accounts-data/update 更新业务数据
     * @apiGroup AccountsData
     * @apiName update
     * @apiUse CommonParam
     *
     * @apiBody {Number}   id                                               列表ID【编辑时必传】
     * @apiBody {String}   date_time                                        日期
     * @apiBody {Number}   platform_id                                      平台ID
     * @apiBody {Number}   account_id                                       账号ID
     * @apiBody {Number}   private_sms                                      私信
     * @apiBody {Number}   clue                                             线索
     * @apiBody {Number}   effective_clue                                   有效线索
     * @apiBody {Number}   arrive                                           到诊
     *
     * @apiSuccess {Number} data    新增/编辑ID
     */
    public function actionUpdate()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['date_time']) || !$params['date_time']) {
            return CommonHelper::api(422, '日期必填');
        }
        if (!isset($params['platform_id']) || !$params['platform_id']) {
            return CommonHelper::api(422, '平台必填');
        }
        if (!isset($params['account_id']) || !$params['account_id']) {
            return CommonHelper::api(422, '账号必填');
        }
        if ((isset($params['private_sms']) && !$params['private_sms']) &&
            (isset($params['clue']) && !$params['clue']) &&
            (isset($params['effective_clue']) && !$params['effective_clue']) &&
            (isset($params['arrive']) && !$params['arrive'])) {
            return CommonHelper::api(422, '数据至少填写一项');
        }
        $params['id'] = $params['id'] ?? 0;
        $dataService = new AccountsDataService();
        return $dataService->updateAccountsData($params);
    }

    /**
     * @api {post} /v1/buss/accounts-data/delete 删除业务数据
     * @apiGroup AccountsData
     * @apiName delete
     * @apiUse CommonParam
     *
     * @apiBody {String} id     ID
     *
     * @apiSuccess {Object} data    返回数据
     */
    public function actionDelete()
    {
        $id = Yii::$app->request->post('id');
        if (!isset($id) || !$id) {
            return CommonHelper::api(422, '缺少数据ID');
        }

        $service = new AccountsDataService();
        return $service->delete($id);
    }
}
